import Vue from "vue";
import VueRouter from "vue-router";
import Loading from "@/views/Loading.vue";
import Index from "@/views/Index.vue";
import Sign from "@/views/Sign.vue";
import CN from "@/views/CN/Index.vue";
import EN from "@/views/EN/Index.vue";
import MapCN from "@/views/CN/Map.vue";
import MapEN from "@/views/EN/Map.vue";
import SafetyCN from "@/views/CN/Safety.vue";
import SafetyEN from "@/views/EN/Safety.vue";
import WelcomeCN from "@/views/CN/Welcome.vue";
import WelcomeEN from "@/views/EN/Welcome.vue";
import CNItemA from "@/views/CN/views/ItemA.vue";
import CNItemB from "@/views/CN/views/ItemB.vue";
import CNItemC from "@/views/CN/views/ItemC.vue";
import CNItemD from "@/views/CN/views/ItemD.vue";
import CNItemE from "@/views/CN/views/ItemE.vue";
import CNItemF from "@/views/CN/views/ItemF.vue";
import CNItemG from "@/views/CN/views/ItemG.vue";
import CNItemH from "@/views/CN/views/ItemH/index.vue";
import LabCN from "@/views/CN/Lab.vue";
import LabEN from "@/views/EN/Lab.vue";
import ENItemA from "@/views/EN/views/ItemA.vue";
import ENItemB from "@/views/EN/views/ItemB.vue";
import ENItemC from "@/views/EN/views/ItemC.vue";
import ENItemD from "@/views/EN/views/ItemD.vue";
import ENItemE from "@/views/EN/views/ItemE.vue";
import ENItemF from "@/views/EN/views/ItemF.vue";
import ENItemG from "@/views/EN/views/ItemG.vue";
import ENItemH from "@/views/EN/views/ItemH/index.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "load",
    component: Loading,
  },
  {
    path: "/index",
    name: "index",
    component: Index,
  },
  {
    path: "/sign",
    name: "sign",
    component: Sign,
  },
  {
    path: "/cn",
    component: CN,
    children: [
      {
        path: "",
        component: WelcomeCN,
      },
      {
        path: "map",
        component: MapCN,
      },
      {
        path: "safety",
        component: SafetyCN,
      },
      {
        path: "lab",
        component: LabCN,
        children: [
          {
            path: "itemA",
            component: CNItemA,
          },
          {
            path: "itemB",
            component: CNItemB,
          },
          {
            path: "itemC",
            component: CNItemC,
          },
          {
            path: "itemD",
            component: CNItemD,
          },
          {
            path: "itemE",
            component: CNItemE,
          },
          {
            path: "itemF",
            component: CNItemF,
          },
          {
            path: "itemG",
            component: CNItemG,
          },
          {
            path: "itemH",
            component: CNItemH,
          },
        ],
      },
      {
        path: "contact",
        component: () => import("@/views/CN/Contact.vue"),
      },
      {
        path: "download",
        component: () => import("@/views/CN/Download.vue"),
      },
    ],
  },
  {
    path: "/en",
    component: EN,
    children: [
      {
        path: "",
        component: WelcomeEN,
      },
      {
        path: "safety",
        component: SafetyEN,
      },
      {
        path: "map",
        component: MapEN,
      },
      {
        path: "lab",
        component: LabEN,
        children: [
          {
            path: "itemA",
            component: ENItemA,
          },
          {
            path: "itemB",
            component: ENItemB,
          },
          {
            path: "itemC",
            component: ENItemC,
          },
          {
            path: "itemD",
            component: ENItemD,
          },
          {
            path: "itemE",
            component: ENItemE,
          },
          {
            path: "itemF",
            component: ENItemF,
          },
          {
            path: "itemG",
            component: ENItemG,
          },
          {
            path: "itemH",
            component: ENItemH,
          },
        ],
      },
      {
        path: "contact",
        component: () => import("@/views/EN/Contact.vue"),
      },
      {
        path: "download",
        component: () => import("@/views/EN/Download.vue"),
      },
    ],
  },
  {
    path: "pdf",
    component: () => import("@/views/PDF.vue"),
  },
  {
    path: "img",
    component: () => import("@/views/Img.vue"),
  },
  {
    path:'/drag',
    component: () => import("@/views/Drag.vue"),
  }
];

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    return { y: 0 };
  },
});

//全局路由守卫 应用场景：后台管理系统，必须登录才能跳转到主页面
// router.beforeEach((to, from, next) => {
//   console.log(to)
//   console.log(from)
//   const tokenStr = localStorage.getItem('token')
//   if (to.path === '/home' && !tokenStr) {
//     next('/login')
//   } else {
//     next()
//   }
// })

// 先把vueRouter原型对象的push、replace保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
// 重写push和replace
// 第一个参数，告诉原来push方法，往哪里跳转，传递哪些参数
// 第二个参数是成功的回调
// 第三个参数是失败的回调
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    // call和apply区别
    // 相同点：都可以调用函数一次，都可以篡改函数的上下文一次
    // 不同点：call和apply传递参数：call传递参数用逗号隔开，apply方法传递数组
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};
export default router;
